<?php
/**
 * 用户处理model
 * Created by Forever.
 * User: Forever
 * Date: 2018/10/03 
 * Time: 上午 10:39
 */
namespace app\api\model;
use think\Cache;
use \think\Model;
use \think\db;
use EasyWeChat\Foundation\Application;
class Member extends Base{

    public function initialize() { } 

    /*
    * 获取会员详情
    * @param data
    * @return object
    */
    public function getMemberDetail($data=[]){
        $where = [
            "openid"=>$data['openid']
        ];
        $field = "id,name,nickname,cs_id,mobile,headimg,province,city,district,address,addtime";
        $res =  Db::name(self::$tb['member'])->where($where)->field($field)->find();
        if($res){
            return $res;
        }else{
            return false;
        }
    }

   /*
    * 获取订单会员详情
    * @param data
    * @return object
    */
    public function memberDetail($data){
       $where=[ "id"=>$data['member_id']];
       $field ="id,nickname,headimg,name,sex,mobile";
       $detail = Db::name(self::$tb['member'])->where($where)->field($field)->find();
       $detail['car'] = Db::name(self::$tb['order'])->where('member_id',$data['member_id'])->value("title");
       return $detail;
    }


   /*
    * 获取店铺列表
    * @param data
    * @return object
    */
    public function  getShopList($data=[]){
        $where = [ "s.ischeck"=>1]; 
        $data['page'] = isset($data['page'])?$data['page']:1;
        $data['rows'] = isset($data['rows'])?$data['rows']:10;
        $field = "s.id,s.name,s.province,s.city,s.district,s.detail,s.shop_pic,f.addtime";
        $result['total'] =  Db::name(self::$tb['follow_shop'])->alias("f")->join("jun_shop s",'s.id=f.shop_id')
        ->where($where)->count(); 
        $list =  Db::name(self::$tb['follow_shop'])->alias("f")->join("jun_shop s",'s.id=f.shop_id')
        ->where($where)->field($field)->page($data['page'],$data['rows'])->select(); 
        if(!empty($list)){
            foreach ($list as $key => $value) {
                $prepAddr = $value['province'].$value['city'].$value['district'].$value['detail'];
                $list[$key]['addr'] = $prepAddr;
                $list[$key]['latng'] = $this->get_localtion($data['latitude'],$data['longitude'],$prepAddr); 
            }
        }
        $result['list'] = $list;
        return $result??[];
    }

   /*
    * 获取店铺详情
    * @param data
    * @return object
    */
    public function getShopDetail($data=[]){
        $where = [ "id" =>$data['id']];
        $field = "id,name,province,city,district,detail";
        $detail = Db::name(self::$tb['shop'])->where($where)->field($field)->find();
        if(!empty($detail)){
            return $detail;
        }else{
            return false;
        }
    }

   /*
    * 获取该店铺的产品列表
    * @param data
    * @return object
    */
    public function getShopCarList($data=[]){
      // $where['shop_id'] = $data['id'];
        $where = [];
        $data['page'] = isset($data['page'])?$data['page']:1;
        $data['rows'] = isset($data['rows'])?$data['rows']:10;
        $field = "p.id,p.name,p.pic,p.low,p.payments,p.supply,c.name as class_name";
        $list['total']=Db::name(self::$tb['product'])->alias("p")->join("jun_product_class c","c.id=p.uid","left")->where($where)->count(); 
        $list['list']=Db::name(self::$tb['product'])->alias("p")->join("jun_product_class c","c.id=p.uid","left")->where($where)->field($field)->page($data['page'],$data['rows'])->select(); 
        return $list??[];
    }


    

   /*
    * 获取会员收藏的店铺列表
    * @param data
    * @return object
    */
    public function getShopFollow($data=[]){ 
        $where = [
            "f.member_id"=>$data['member_id'],
            "s.ischeck"=>1
        ]; 
        $data['page'] = isset($data['page'])?$data['page']:1;
        $data['rows'] = isset($data['rows'])?$data['rows']:10;
        $field = "s.id,s.name,s.province,s.city,s.district,s.detail,s.shop_pic,f.addtime";
        $result['total'] =  Db::name(self::$tb['follow_shop'])->alias("f")->join("jun_shop s",'s.id=f.shop_id')
        ->where($where)->count(); 
        $list =  Db::name(self::$tb['follow_shop'])->alias("f")->join("jun_shop s",'s.id=f.shop_id')
        ->where($where)->field($field)->page($data['page'],$data['rows'])->select(); 
        if(!empty($list)){
            foreach ($list as $key => $value) {
                $prepAddr = $value['province'].$value['city'].$value['district'].$value['detail'];
                $list[$key]['addr'] = $prepAddr;
                $list[$key]['latng'] = $this->get_localtion($data['latitude'],$data['longitude'],$prepAddr); 
            }
        }
        $result['list'] = $list;
        return $result??[];
    }

   /*
    * 获取定位
    */
    public function get_localtion($latitude,$longitude,$prepAddr){  
       $data = $this->data;
       $key = config("baiduKey"); 
       $latng = getLatng($prepAddr); 
       if($latng){
           $url = "http://api.map.baidu.com/routematrix/v2/driving?output=json&origins=".$latitude.",".$longitude."&destinations=".$latng['lat'].",".$latng['lng']."&ak=".$key;  
           $geocode = file_get_contents($url);
           $output= json_decode($geocode,true);  
           if(!empty($output['result'])){
              $latng = $output['result'][0]['distance']['text']; 
              return $latng;
            }else{
              return "";
            }
       }else{
        return "";
       }
    }
    

  /*
    * 获取会员收藏的汽车列表
    * @param data
    * @return object
    */
    public function getCarFollow($data=[]){
        $where = ["member_id"=>$data['member_id']];
        $data['page'] = isset($data['page'])?$data['page']:1;
        $data['rows'] = isset($data['rows'])?$data['rows']:10;
        $field = "id,product_id,title,pic,first_pay,monthly_pay,attr,addtime";
        $list['list'] =  Db::name(self::$tb['follow_product'])->where($where)->field($field)->order("addtime desc")->page($data['page'],$data['rows'])->select();  
        $list['total'] = Db::name(self::$tb['follow_product'])->where($where)->count();
        return $list; 
    }

  /*
    * 获取会员浏览列表
    * @param data
    * @return object
    */
    public function getBrowsing($data=[]){
        $where = ["h.member_id"=>$data['member_id']];
        $data['page'] = isset($data['page'])?$data['page']:1;
        $data['rows'] = isset($data['rows'])?$data['rows']:10;
        $field = "p.id,h.product_id,p.name as title,p.pic,h.addtime";
        $list['list'] =  Db::name(self::$tb['browsing_history'])->alias("h")->join("jun_product p","p.id = h.product_id","left")
        ->where($where)->field($field)->page($data['page'],$data['rows'])->select();
        foreach ($list['list'] as $key => $value) {
            $list['list'][$key]['pic'] = json_decode($value['pic'],true)['img'];
        }
        $list['total'] = Db::name(self::$tb['browsing_history'])->alias("h")->join("jun_product p","p.id = h.product_id","left")->where($where)->count();
        return $list; 
    }

    /*
    * 会员收藏店铺
    * @param data
    * @return boolean
    */
    public function followShop($data=[]){
      $where = ['id'=>$data['id']];
      $shop = Db::name(self::$tb['shop'])->where($where)->value("id");
      if(!$shop){
        return -10023;
      }
      $follow = [
        "member_id"=>$data['member_id'],
        "shop_id"=>$data['id']
      ];
      $exist = Db::name(self::$tb['follow_shop'])->where($follow)->value("id");
      if($exist){
        $follow['addtime'] = time();
        $result = Db::name(self::$tb['follow_shop'])->where("id",$exist)->update($follow);
      }else{
        $follow['addtime'] = time();
        $result = Db::name(self::$tb['follow_shop'])->insert($follow);
      }
     
     if($result){
        return true;
     }else{
       return false;

     }
    }

    



 }   
